package com.example.demo.controller.common;

import lombok.AllArgsConstructor;
import lombok.Builder;

import java.io.Serializable;

/**
 * 统一响应格式
 * @author baisongyuan
 * @className CustomResponse
 * @description
 * @date 2022/9/20 21:16
 */
@AllArgsConstructor
@Builder
public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> success(T data) {
        return Result.<T>builder()
                .data(data)
                .code(StatusCodeEnum.SUCCESS.getCode())
                .msg(StatusCodeEnum.SUCCESS.getMsg())
                .build();
    }

    public static <T> Result<T> fail() {
        return Result.<T>builder().code(StatusCodeEnum.FAIL.getCode()).msg(StatusCodeEnum.FAIL.getMsg()).build();
    }

    public static <T> Result<T> fail(T data) {
        return Result.<T>builder().code(StatusCodeEnum.FAIL.getCode()).msg(StatusCodeEnum.FAIL.getMsg())
                .data(data)
                .build();
    }
}
